package task;

import java.util.ArrayList;

import com.sisuntech.common.ValueBase;
import com.sisuntech.db.DbUtil;
import com.sisuntech.task.TaskBase;

import exec.ConvertToFlv;

public class ConvertToFlvTask implements TaskBase {
	public void run() {
		ArrayList tList = DbUtil.getValueBase("select videoId, uploadFile from video_info where status = '0'", 0, 10);
		for(int i=0; tList != null && i < tList.size(); i++){
			ValueBase vb = (ValueBase)tList.get(i);
			String inPath = "D:\\newsite\\DeepRed\\WebRoot\\video\\upload\\" + vb.getStringValue("uploadFile");
			String outPath = "D:\\newsite\\DeepRed\\WebRoot\\video\\publish\\";
			ConvertToFlv.convert(inPath, outPath + vb.getStringValue("videoId") + ".flv");
			StringBuffer logBuf = new StringBuffer();
			ConvertToFlv.genSnapshot(outPath + vb.getStringValue("videoId") + ".flv", outPath + vb.getStringValue("videoId") + "-120x72.jpg", 120, 72, null);
			ConvertToFlv.genSnapshot(outPath + vb.getStringValue("videoId") + ".flv", outPath + vb.getStringValue("videoId") + ".jpg", 0, 0, logBuf);
			String stDuration = logBuf.toString();
			int tIndex = stDuration.indexOf("Duration:");
			System.out.println("Duration Index = " + tIndex);
			if(tIndex >= 0)stDuration = stDuration.substring(tIndex + 9).trim();
			else stDuration = "";
			tIndex = stDuration.indexOf(",");
			if(tIndex > 0)stDuration = stDuration.substring(0, tIndex);
			if(stDuration.startsWith("00:"))stDuration = stDuration.substring(3);
			tIndex = stDuration.indexOf(".");
			if(tIndex > 0)stDuration = stDuration.substring(0, tIndex);
			DbUtil.executeSql("update video_info set status = '1', duration = '" + stDuration + "' where videoId = " + vb.getStringValue("videoId"));
		}
	}

}
